package com.electric.qrapp.utils.view

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator

@Deprecated("用的NewLoadingView")
class LoadingView(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {
    /**
     * 内外弧的宽高
     */
    private var circleWidth = 10

    /**
     * 内弧颜色
     */
    private var InnerColor = Color.BLUE

    /**
     * 外弧颜色
     */
    private var foreginColor = Color.RED

    /**
     * 圆角颜色，也可在layout中设置背景，如果自己设置背景的话还要加圆角，这里已经是处理好了
     */
    private val rectColor = Color.parseColor("#999999")

    /**
     * 外弧画笔
     */
    private var paintforegin: Paint? = null

    /**
     * 内弧画笔
     */
    private var paintInner: Paint? = null

    /**
     * 背景圆角画笔
     */
    private var paintRect: Paint? = null

    /**
     * 外弧起始与终止点
     */
    private var startForegin = 135
    private val endForegin = 275

    /**
     * 内弧起始与终止点
     */
    private var startInner = 45
    private val endInner = 90

    /**
     * 内外弧半径
     */
    private val radus = 150
    private val innerPading = radus - 70

    /**
     * 背景圆角的半径
     */
    private val rectRadus = radus + 50

    /**
     * 背景圆角大小
     */
    private val rectCircle = 30

    private val valueAnimator: ValueAnimator by lazy {
        ValueAnimator.ofInt(0, 2).apply {
            duration = 1
            repeatCount = ValueAnimator.INFINITE
            repeatMode = ValueAnimator.RESTART
            interpolator = LinearInterpolator()

        }
    }

    init {

        initCirclePaint()

    }

    /**
     * 初始化画笔
     */
    private fun initCirclePaint() {
        paintforegin = Paint()
        paintforegin!!.isAntiAlias = true
        paintforegin!!.isDither = true
        paintforegin!!.strokeWidth = circleWidth.toFloat()
        paintforegin!!.color = foreginColor
        paintforegin!!.style = Paint.Style.STROKE
        paintforegin!!.strokeCap = Paint.Cap.ROUND
        paintforegin!!.strokeJoin = Paint.Join.ROUND
        paintInner = Paint()
        paintInner!!.isAntiAlias = true
        paintInner!!.isDither = true
        paintInner!!.strokeWidth = circleWidth.toFloat()
        paintInner!!.color = InnerColor
        paintInner!!.style = Paint.Style.STROKE
        paintInner!!.strokeJoin = Paint.Join.ROUND
        paintInner!!.strokeCap = Paint.Cap.ROUND
        paintRect = Paint()
        paintRect!!.isAntiAlias = true
        paintRect!!.isDither = true
        paintRect!!.color = rectColor
        paintRect!!.style = Paint.Style.FILL
    }

    /**
     * 测量处理
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //获取宽高的模式
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        //指定宽高的大小
        var width = MeasureSpec.getSize(widthMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)
        //如果宽高设置为wrap_content时，刚默认为300
        if (widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.AT_MOST) {
            width = 300
            height = width
        }
        //如果宽高不一致时，则以宽为标准
        if (width != height) {
            height = width
        }
        setMeasuredDimension(width, height)
    }

    /**
     * 中心点
     */
    private var y = 0
    private var x = 0
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        x = (right - left) / 2
        y = (bottom - top) / 2
        super.onLayout(changed, left, top, right, bottom)
    }



    override fun onDraw(canvas: Canvas) {
//        drawCircle(canvas)
        drawArc(canvas)
        drawArc2(canvas)
    }

    /**
     * 绘制外弧
     * @param canvas
     */
    private fun drawArc(canvas: Canvas) {
        val rectF = RectF(
            (x - radus).toFloat(), (y - radus).toFloat(),
            (x + radus).toFloat(), (y + radus).toFloat()
        )
        canvas.drawArc(rectF, startForegin.toFloat(), endForegin.toFloat(), false, paintforegin!!)
    }

    /**
     * 绘制内弧
     * @param canvas
     */
    private fun drawArc2(canvas: Canvas) {
        val rectF = RectF(
            (x - innerPading).toFloat(),
            (y - innerPading).toFloat(), (x + innerPading).toFloat(), (y + innerPading).toFloat()
        )
        canvas.drawArc(rectF, startInner.toFloat(), endInner.toFloat(), false, paintInner!!)
    }

//    /**
//     * 绘制背景圆角
//     * @param canvas
//     */
//    private fun drawCircle(canvas: Canvas) {
//        val rectF = RectF(
//            (x - rectRadus).toFloat(),
//            (y - rectRadus).toFloat(), (x + rectRadus).toFloat(), (y + rectRadus).toFloat()
//        )
//        canvas.drawRoundRect(rectF, rectCircle.toFloat(), rectCircle.toFloat(), paintRect!!)
//    }

    /**
     * 绘制更新
     * @param value
     */
    private fun reDraw(value: Int) {
        startForegin += value
        startInner -= value
        postInvalidate()
    }

    /**
     * 设置属性动画来重新绘制
     */
    @SuppressLint("WrongConstant")
    fun loading() {
        if (!valueAnimator.isStarted) {
            valueAnimator.start()
            valueAnimator.addUpdateListener {
                val value = it.animatedValue as Int
                reDraw(value)
            }
        }
    }

    private fun dismiss() {
        if (isShow()) {
            valueAnimator.cancel()
            valueAnimator.removeAllUpdateListeners()
        }
    }


    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        loading()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        dismiss()

    }

    private fun isShow() = valueAnimator.isRunning


}


